﻿// Project: Salient
// http://salient.codeplex.com
// 
// Copyright 2010, Sky Sanders <sky at skysanders.net>
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://salient.codeplex.com/license
// 
// Date: April 24 2010 

#region

using System;
using System.Data;
using System.Data.Common;
using System.Xml;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

#endregion

/// http://stackoverflow.com/questions/242022/nhibernate-mapping-to-custom-types
/// 
/// http://www.martinwilley.com/net/code/nhibernate/usertype.html
/// http://www.martinwilley.com/net/code/nhibernate/usertype.html
/// 
/// http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx
/// 
/// http://orbitalcoding.nolior.se/post/Fluent-NHibernate-Automapping-and-the-MySql-DateTime.aspx
/// 
/// http://stackoverflow.com/questions/1192386/mapping-xml-data-type-with-nhibernate
/// 
/// http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/
/// 
/// http://randomcode.net.nz/2009/10/16/simplify-iusertype-testing/
/// 
/// http://www.adverseconditionals.com/2009/01/automatically-serializing-value-objects.html - looks good but check the update 
///     http://refactormycode.com/codes/710-polymorphic-xml-serializer#refactor_144121
/// 
/// 
/// 
/// http://www.codeinsanity.com/2009/02/using-fluent-nhibernate-in-rhinestone_12.html
///     http://geekswithblogs.net/opiesblog/archive/2006/08/13/87880.aspx
/// 
namespace Salient.SqlServer.NHibernate
{
    /// <summary>
    /// started with http://gist.github.com/47082
    /// </summary>
    public class XmlDocumentUserType : IUserType
    {
        #region IUserType Members

        public new bool Equals(object x, object y)
        {
            if (x == null || y == null)
                return false;

            XmlDocument xdoc_x = (XmlDocument) x;
            XmlDocument xdoc_y = (XmlDocument) y;
            return xdoc_y.OuterXml == xdoc_x.OuterXml;
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }


        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            if (names.Length != 1)
                throw new InvalidOperationException("names array has more than one element. can't handle this!");

            XmlDocument document = new XmlDocument();

            string val = rs[names[0]] as string;

            if (val != null)
            {
                document.LoadXml(val);
                return document;
            }

            return null;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            DbParameter parameter = (DbParameter) cmd.Parameters[index];

            if (value == null)
            {
                parameter.Value = DBNull.Value;
                return;
            }

            parameter.Value = ((XmlDocument) value).OuterXml;
        }

        public object DeepCopy(object value)
        {
            XmlDocument toCopy = value as XmlDocument;

            if (toCopy == null)
                return null;

            XmlDocument copy = new XmlDocument();
            copy.LoadXml(toCopy.OuterXml);
            return copy;
        }

        public object Replace(object original, object target, object owner)
        {
            throw new NotImplementedException();
        }

        public object Assemble(object cached, object owner)
        {
            string str = cached as string;
            if (str != null)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(str);
                return doc;
            }
            else
            {
                return null;
            }
        }

        public object Disassemble(object value)
        {
            XmlDocument val = value as XmlDocument;
            if (val != null)
            {
                return val.OuterXml;
            }
            else
            {
                return null;
            }
        }

        public SqlType[] SqlTypes
        {
            get { return new SqlType[] {new SqlXmlStringType()}; }
        }

        public Type ReturnedType
        {
            get { return typeof (XmlDocument); }
        }

        public bool IsMutable
        {
            get { return true; }
        }

        #endregion
    }
}